<template>
<div>
  <el-breadcrumb separator="/">
    <el-breadcrumb-item :to="{ path: '/' }" style="font-size: 16px">
      <i class="el-icon-s-promotion" />后台管理
    </el-breadcrumb-item>
    <el-breadcrumb-item>添加属性模板</el-breadcrumb-item>
  </el-breadcrumb>

  <el-divider></el-divider>

  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="名称" prop="name">
      <el-input v-model="ruleForm.name"></el-input>
    </el-form-item>
    <el-form-item label="拼音" prop="description">
      <el-input v-model="ruleForm.pinyin"></el-input>
    </el-form-item>
    <el-form-item label="关键词列表" prop="keywords">
      <el-input v-model="ruleForm.keywords"></el-input>
    </el-form-item>
    <el-form-item label="排序序号" prop="sort">
      <el-input v-model="ruleForm.sort"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</div>
</template>

<script>

export default {
  data() {
    return {
      ruleForm: {
        name: '测试属性模板名称001',
        pinyin:"测试属性模板简介001",
        keywords:"测试关键词1，测试关键词2，测试关键词3",
        sort:99

      },
      rules: {
        name: [
          { required: true, message: '请输入属性模板名称', trigger: 'blur' },
          { min: 3, max: 25, message: '长度在 3 到 25 个字符', trigger: 'blur' }
        ],
        pinyin: [
          { required: true, message: '请输入拼音', trigger: 'blur' },
          { pattern: /^[a-z]+$/, message: '长度在 1 到 100 个字子母', trigger: 'blur' }
        ],
        keywords: [
          { required: true, message: '请输入关键字', trigger: 'blur' },
          { min: 1, max: 35, message: '长度在 1 到 35 个字符', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入排序序号', trigger: 'blur' },
          {pattern:/^(\d{1}|[1-9]{1}[0-9]?)$/, message:'相册排序序号必须是0-99之间的数字',  trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url='http://localhost:8088/attribute-template/add-new';
          console.log('url='+url);
          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = '+ formData);
          this.axios.post(url, formData).then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000){
              this.$message({
                message: '添加成功！',
                type: 'success'
              });
              this.resetForm(formName);
            }else{
              this.$message.error('添加失败，尝试添加的属性模板名已被占用');
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style>

</style>